package com.example.ymm.modules1.service.impl.hl.ex;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.hl.ex.ExAnserLog;
import com.example.ymm.modules1.entity.hl.ex.ExPublishGroup;
import com.example.ymm.modules1.entity.hl.ex.ExPublishPaper;
import com.example.ymm.modules1.mapper.ex.ExAnserLogMapper;
import com.example.ymm.modules1.query.hl.ex.ExAnserLogQueryCriteria;
import com.example.ymm.modules1.service.hl.ex.ExAnserLogService;
import com.example.ymm.util.QueryHelpPlus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@DSTransactional
@Slf4j
@DS("slave2")
public class ExAnserLogServiceImpl extends BaseServiceImpl<ExAnserLogMapper, ExAnserLog> implements ExAnserLogService {

    @Override
    public TableList<ExAnserLog> list(ExAnserLogQueryCriteria criteria, YmPageDto pageDto) {
        IPage<ExAnserLog> page = new Page<ExAnserLog>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(ExAnserLog.class, criteria);
        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<ExAnserLog> tableList=new TableList(page);
        return tableList;
    }

    public List<ExAnserLog> queryAll(ExAnserLogQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(ExAnserLog.class, criteria);
        predicate.orderByDesc("create_time");
        List<ExAnserLog> result =baseMapper.selectList(predicate);
        return result;
    }

    @Override
    public String addFn(ExPublishPaper publishPaper) {
        ExPublishGroup startGroup = publishPaper.getStartGroup();

        ExAnserLog exAnserLog = new ExAnserLog();
        exAnserLog.setStatus(0);
        exAnserLog.setPublishGroupId(startGroup.getPublishGroupId());
        exAnserLog.setPublishPaperId(publishPaper.getPublishPaperId());
        exAnserLog.setTextJson(JSONObject.toJSONString(publishPaper));
        exAnserLog.setUserId(getUserId());
        exAnserLog.setUserName(getUserName());
        saveOrUpdate(exAnserLog);
        return exAnserLog.getId();
    }

    @Override
    public void finishFn(String id) {
        ExAnserLog exAnserLog = new ExAnserLog();
        exAnserLog.setId(id);
        exAnserLog.setStatus(1);
        exAnserLog.setUpdateTime(new Date());
        baseMapper.updateById(exAnserLog);
    }
}
